package com.lagou.wab.servlet;

import com.alibaba.fastjson.JSON;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.impl.CourseServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {
        // 写方法
        public void findCourseList(HttpServletRequest req, HttpServletResponse resp){

            try {
                CourseService cs =  new CourseServiceImpl();
                // 直接返回结果
                List<Course> courseList = cs.findCourseList();
                // 转json
                String s = JSON.toJSONString(courseList);
                // 响应出去
                resp.getWriter().print(s);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


//        /*
//         *  条件查询
//         * */
//
//        public void findCourseNameAndStatus(HttpServletRequest req, HttpServletResponse resp){
//            try {
//                String course_name = req.getParameter("course_name");
//                String status = req.getParameter("status");
//
//                CourseService courseService =  new CourseServiceImpl();
//                List<Course> list= courseService.findCourseNameAndStatus(course_name, status);
//                String s = JSON.toJSONString(list);
//
//                resp.getWriter().print(s);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//
//        }
//        /*
//        * 创建课程
//        * */
//
//        public void createCourse(HttpServletRequest req, HttpServletResponse resp){
//            String course_name = req.getParameter("course_name");
//            String price = req.getParameter("price");
//
//            CourseService cs = new CourseServiceImpl();
////            javaBean 把接受的参数封装到对象中
////            cs.saveCourseSalesInfo()
//
//
//        }


    //获取id
    public void findCourseById(HttpServletRequest req,HttpServletResponse resp){
        try {
            //获取客户端的请求参数 id
        String id = req.getParameter("id");
        CourseService cs = new CourseServiceImpl();

        //int
        int i = Integer.parseInt(id);
        List<Course> list= cs.findCourseById(i);

        //对象转json
        String s = JSON.toJSONString(list);
            resp.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

